package com.apiauto.test_case;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.testng.Assert;
import org.testng.AssertJUnit;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.alibaba.fastjson.JSONObject;
import com.apiauto.data_util.CaseUtil;
import com.apiauto.data_util.DBCheckUtil;
import com.apiauto.data_util.ExcelUtil;
import com.apiauto.data_util.RestUtil;
import com.apiauto.dto.CaseDTO;
import com.apiauto.http_util.Request;
import com.apiauto.http_util.Response;
import com.apiauto.log.Log;
import com.apiauto.pojo.Rest;
import com.apiauto.pojo.WriteData;
import com.apiauto.util.TestNGRetry;
import com.google.common.base.Strings;

public class API_001_Login extends BaseCase {
	/**
	 * PS:公有测试方法
	 * @param apiNum 接口编号
	 * @param caseNum 用例编号
	 * @param requestHeaders 请求头
	 * @param requestDataType 数据类型
	 * @param requestData 请求数据
	 * @param checkPoint 检查点
	 * @param expectResponseResult 预期响应结果
	 * @param correlation 提取参数公式
	 * @param checkExcuteSql 检查脚本
	 */
	@Test(dataProvider = "datas",testName = "登录",retryAnalyzer= TestNGRetry.class)
	public void baseTest(String apiNum, String caseNum, String requestHeaders, String requestDataType,
			String requestData, String checkPoint, String expectResponseResult, String correlation,
			String checkExcuteSql) {
		//发送请求以及脚本校验,获取响应对象
		CloseableHttpResponse response = sendRequest(apiNum, caseNum, requestHeaders, requestDataType, requestData, checkExcuteSql);
		// 获取响应头
		String headers = JSONObject.toJSONString(Response.getResponseHeaders(response));
		// 获取响应内容
		String responseBody = Response.getResponseEntity(response);
		// 判断是否存在提取变量公式,若存在将变量存入集合
		if (!Strings.isNullOrEmpty(correlation)) {
			// 提取依赖参数
			CaseUtil.extractVariable(headers, correlation);
		}
		//断言及写入
		assertAndWrite(caseNum, responseBody, checkPoint);

	}
	@DataProvider(name = "datas")
	public Object[][] datas() {
		Object[][] datas = CaseUtil.getExcelEntity("apiNum", "api-001", ExcelUtil.caseDTOList, ExcelUtil.caseDTOfields,
				CaseDTO.class);
		return datas;
	}

}
